<?php

class Application_Model_dataDAO_ChucNangSanPhamDAO implements Application_Model_dataDAOInf_ChucNangSanPhamDAOInf {
	protected static $_instance=null;
	
	public static function getInstance(){
		if(self::$_instance==null) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	/**
	 * @param String $tenChucNang
	 * @param String $moTa
	 * @param String $hinhIcon
	 */
	public function luuChucNangSanPham($tenChucNang, $moTa, $hinhIcon) {
		$result=0;
		$data=array("tenChucNang" =>$tenChucNang,
				"hinhIcon" => $hinhIcon,
				"moTa" => $moTa
			);
		$result=Application_Model_dataDAO_DBConnect::insertQuery('tb_chucnangsanpham',$data);
		return $result;
	}
	/**
	 * @param String $tenChucNang
	 * @param String $moTa
	 * @param String $hinhIcon
	 */
	public function updateChucNangSanPham($id,$tenChucNang, $moTa, $hinhIcon) {
		$result=false;
			$update=array("tenChucNang" => $tenChucNang,"hinhIcon" => $hinhIcon,"moTa" => $moTa);
			$conditions[] = 'maChucNangSanPham='.$id;
		$result=Application_Model_dataDAO_DBConnect::updateQuery('tb_chucnangsanpham',$update,$conditions);
		return $result;	
	}
	/**
	 * @param int $id
	 */
	public function getChucNangSanPham($id) {
		$statment="SELECT * FROM tb_chucnangsanpham WHERE maChucNangSanPham=".$id;
		$result=null;
		$arr=Application_Model_dataDAO_DBConnect::selectQuery($statment);
		foreach ($arr as $value) {
			$result= new Application_Model_bean_ChucNangSanPham($value['tenChucNang'],$value['hinhIcon'],$value['moTa']);
		}
		return $result;
	}
	/**
	 * 
	 */
	public function getAllChucNangSanPham() {
		$resutl=array();
		$statment="SELECT * FROM tb_chucnangsanpham";
		$arr=Application_Model_dataDAO_DBConnect::selectQuery($statment);
		$i=0;
		foreach ($arr as $value) {
			$item=new Application_Model_bean_ChucNangSanPham($value['tenChucNang'],$value['hinhIcon'],$value['moTa']);
			$item->setTrangThai($value['trangThai']);
			$item->setId($value['maChucNangSanPham']);
			$resutl[$i]=$item;
			$i++;
		}
		return $resutl;
	}
	/**
	 * @param unknown_type $idNhomChucNang
	 * @param unknown_type $noiDung
	 */
	public function luuItemChucNangSanPham($idNhomChucNang, $noiDung) {
		$result=0;
		$data=array("noiDung" => $noiDung, "maChucNangSanPham" => $idNhomChucNang);
		$result=Application_Model_dataDAO_DBConnect::insertQuery('tb_chucnangsanphamitem',$data);
		return $result;
	}
	/**
	 * 
	 */
	public function getAllItemChucNangSanPham() {
		//lay het cac nhom chuc nang
		$listNhomChucNang=array();
		$listNhomChucNang=$this->getAllChucNangSanPham();
		foreach ($listNhomChucNang as $value) {
			$idNhomChucNang=$value->getId();
			$listItem=$this->getAllItemByNhomChucNang($idNhomChucNang);
			$value->setListItem($listItem);
		}
		return $listNhomChucNang;
	}

	/**
	 * @param unknown_type $id
	 */
	public function getItemChucNangSanPham($id) {
		$statment="SELECT * FROM tb_chucnangsanphamitem WHERE maItem=".$id;
		$result=null;
		$arr=Application_Model_dataDAO_DBConnect::selectQuery($statment);
		foreach ($arr as $value) {
			$result= new Application_Model_bean_ItemChucNangSanPham();
			$result->setId($value['maItem']);
			$result->setNoiDung($value['noiDung']);
			$result->setIdNhomChucNang($value['maChucNangSanPham']);
		}
		return $result;
	}

	/**
	 * @param unknown_type $id
	 * @param unknown_type $idNhomChucNang
	 * @param unknown_type $noiDung
	 */
	public function updateItemChucNangSanPham($id, $idNhomChucNang, $noiDung) {
		$result=false;
			$update=array("noiDung" => $noiDung,"maChucNangSanPham" => $idNhomChucNang);
			$conditions[] = 'maItem='.$id;
		$result=Application_Model_dataDAO_DBConnect::updateQuery('tb_chucnangsanphamitem',$update,$conditions);
		return $result;	
	}
	/**
	 * @param unknown_type $id
	 */
	public function deleteChucNangSanPham($id) {
		//chuyen cac item trong nhom do sang nhom co id bang 1
		$update=array("maChucNangSanPham" => 1);
		$conditions[] = 'maChucNangSanPham='.$id;
		Application_Model_dataDAO_DBConnect::updateQuery('tb_chucnangsanphamitem',$update,$conditions);
		
		//xoa nhom do luon
		$conditions[] = 'maChucNangSanPham='.$id;
		$result=Application_Model_dataDAO_DBConnect::deleteQuery('tb_chucnangsanpham',$conditions);
		return $result;//tra ve so record bi xoa
	}

	/**
	 * @param unknown_type $id
	 */
	public function deleteItemChucNangSanPham($id) {
		$result=0;
		$conditions[] = 'maItem='.$id;
		$result=Application_Model_dataDAO_DBConnect::deleteQuery('tb_chucnangsanphamitem',$conditions);
		return $result;
	}
	/**
	 * @param unknown_type $idNhomChucNang
	 */
	public function getAllItemByNhomChucNang($idNhomChucNang) {
		$result=array();
		$statment="SELECT * FROM tb_chucnangsanphamitem WHERE maChucNangSanPham=".$idNhomChucNang;
		$arr=Application_Model_dataDAO_DBConnect::selectQuery($statment);
		$i=0;
		foreach ($arr as $value) {
			$item=new Application_Model_bean_ItemChucNangSanPham();
			$item->setId($value['maItem']);
			$item->setNoiDung($value['noiDung']);
			$item->setIdNhomChucNang($value['maChucNangSanPham']);
			$result[$i]=$item;
			$i++;
		}
		return $result;
	}


	
	

	
}

?>